//
//  TCLYReservationViewController.m
//  YuErmuYing
//
//  Created by LYang on 16/8/18.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "TCLYReservationViewController.h"
#import "Macros.h"
#import "Masonry.h"
#import "TCLYReservationTableViewCell.h"
#import "TCLYMineModel.h"
#import "TCLYRegisterView.h"
#import "UIImageView+WebCache.h"
#import "TCLYProcessViewController.h"
#import "TCLYUser.h"

@interface TCLYReservationViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView * reservationTableView;
@property (nonatomic, strong) TCLYMineModel * mineModel;
@property (nonatomic, strong) TCLYRegisterView * registerView;
@property (nonatomic, strong) TCLYUser * user;
@end

@implementation TCLYReservationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.backgroundColor = statusColor;
    self.navigationController.navigationBar.barStyle = UIBarMetricsDefaultPrompt;
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, -20, screenWidth, 20)];
    view.backgroundColor = navColor;
    [self.navigationController.navigationBar addSubview:view];
    
    UILabel * titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"预定";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.frame = CGRectMake(0, 0, 50, 20);
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = titleLabel;
    
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(12, 22, 20, 20);
    [leftBtn setImage:[UIImage imageNamed:@"left"] forState:UIControlStateNormal];
    leftBtn.tag = 91;
    [leftBtn addTarget:self action:@selector(statusBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    rightBtn.tag = 92;
    [rightBtn setTitle:@"流程" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    rightBtn.frame = CGRectMake(screenWidth-12-20, 22, 30, 20);
    [rightBtn addTarget:self action:@selector(statusBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createWork) name:@"loadReservationSucceed" object:nil];
    self.mineModel = [[TCLYMineModel alloc] init];
    self.user = [[TCLYUser alloc] init];
    //这个字典封装的是传参的参数
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:self.user.userId, @"uid", nil];
    [self.mineModel loadReservationParameters:dic];
    self.registerView = [[TCLYRegisterView alloc] init];
    [self createView];
}
-(void)createWork{
    [self.reservationTableView reloadData];
}
-(void) createView{
    
    self.reservationTableView = [[UITableView alloc] init];
    self.reservationTableView.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    [self.reservationTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.reservationTableView.dataSource = self;
    self.reservationTableView.delegate = self;
    [self.view addSubview:self.reservationTableView];
    [self.reservationTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view);
        make.right.mas_equalTo(self.view);
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.mineModel.reservationArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * identifier = @"reservation";
    TCLYReservationTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[TCLYReservationTableViewCell alloc] initWithReuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSURL * url = [NSURL URLWithString:self.mineModel.reservationArr[indexPath.row][@"pic"]];
    [cell.headerImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"LYJia2"]];
    
    
    if ([self.mineModel.reservationArr[indexPath.row][@"type"] isEqual:@"1"]) {
        cell.nickNameLabel.text = [NSString stringWithFormat:@"%@(催乳师)",self.mineModel.reservationArr[indexPath.row][@"name"]];
    }else{
        cell.nickNameLabel.text = [NSString stringWithFormat:@"%@(月嫂)",self.mineModel.reservationArr[indexPath.row][@"name"]];
    }
    cell.instructionsLabel.text = self.mineModel.reservationArr[indexPath.row][@"content"];
    cell.sumLabel.text = [NSString stringWithFormat:@"￥%@",self.mineModel.reservationArr[indexPath.row][@"price"]];
    [cell.remindBtn addTarget:self action:@selector(remindBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.remindBtn.tag = 1000+indexPath.row;
    if ([self.mineModel.reservationArr[indexPath.row][@"remind"] isEqual:@"1"]) {
        cell.remindBtn.selected = YES;
    }else{
        cell.remindBtn.selected = NO;
    }
    [cell.cancelBtn addTarget:self action:@selector(cancelBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.cancelBtn.tag = 3000+indexPath.row;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150.0+34/2.0+2+64/2.0;
}

-(void)remindBtnAction:(id)sender{
    
    UIButton * btn = [self.view viewWithTag:((UIButton *) sender).tag];
    if (btn.selected == NO) {
        NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:self.mineModel.reservationArr[((UIButton *) sender).tag-1000][@"appointment_id"],@"appointment_id", nil];
        [self.mineModel remindReservationParameters:dic];
        NSLog(@"%ld",(long)((UIButton *) sender).tag);
        btn.selected = YES;
    }else{
        [self.registerView prompt:@"已经提醒过上门服务哦！" Controller:self];
    }
    
    
    
}
-(void)cancelBtnAction:(id)sender{
    
     NSLog(@"%ld",(long)((UIButton *) sender).tag);
    [self.registerView Prompt:@"您确定要取消其服务吗?" Controller:self];
    self.registerView.determineAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        NSDate *datenow =[NSDate date];//现在时间,你可以输出来看下是什么格式
        NSTimeZone *zone = [NSTimeZone systemTimeZone];
        NSInteger interval = [zone secondsFromGMTForDate:datenow];
        NSDate *localeDate = [datenow  dateByAddingTimeInterval: interval];
        NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[localeDate timeIntervalSince1970]];
        NSLog(@"%@",timeSp);
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cancelBtnAction) name:@"cancelSucced" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cancelAction) name:@"cancelError" object:nil];
        NSLog(@"%@",self.mineModel.reservationArr[((UIButton *)sender).tag-3000][@"appointment_id"]);
        NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys: self.user.userId, @"uid",self.mineModel.reservationArr[((UIButton *)sender).tag-3000][@"appointment_id"],@"appointment_id", nil];
        
        [self.mineModel cancelReservationParameters:dic];
        return ;
    }];
    self.registerView.cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  
    }];
    [self.registerView.promptAlertCtr addAction:self.registerView.determineAction];
    [self.registerView.promptAlertCtr addAction:self.registerView.cancelAction];
}

-(NSString *)getCurrentTime{
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString * dataTime = [formatter stringFromDate:[NSDate date]];
    
    return dataTime;
}
-(void)statusBtnAction:(id)sender{
    switch (((UIButton *) sender).tag) {
        case 91:{
            self.navigationController.navigationBarHidden = YES;
            [self.navigationController popViewControllerAnimated:YES];
            self.tabBarController.tabBar.hidden = NO;
        }
            break;
        case 92:{
            TCLYProcessViewController * processViewCtr = [[TCLYProcessViewController alloc] init];
            [self.navigationController pushViewController:processViewCtr animated:YES];
        }
            break;
    }
}

-(void)cancelBtnAction{
    [self.reservationTableView reloadData];
}

-(void)cancelAction{
    [self.registerView prompt:@"您已超过取消订单的时间" Controller:self];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
